package com.study.quiz.controller;

import com.aliyun.oss.OSSClient;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.study.quiz.dto.FileDTO;
import com.study.quiz.utils.AliyunOssUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author CJunGod
 * @create 2020-03-14 17:21
 **/
@Controller
public class FileController {

    @Resource
    private AliyunOssUtil aliyunOssUtil;

    @ResponseBody
    @PostMapping("/file/upload")
    public FileDTO upload(HttpServletRequest request){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("editormd-image-file");
        String imgUrl = aliyunOssUtil.uploadImg(file);
        FileDTO fileDTO = new FileDTO();
        if(StringUtils.isEmpty(imgUrl)){
            fileDTO.setSuccess(0);
            fileDTO.setMessage("上传失败");
            fileDTO.setUrl("");
        }else {
            fileDTO.setSuccess(1);
            fileDTO.setMessage("成功");
            fileDTO.setUrl(imgUrl);
        }

        return fileDTO;
    }
}
